package com.cooba.activitys.integral;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.bean.UserAddressBean;
import com.cooba.dialog.ChoiceCityDialog;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Created by Administrator on 2016/6/14.
 */
public class UserAddressUpdateActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mUsername_et;
    private EditText mPhone_et;
    private EditText mPostCode_et;
    private TextView mAddress_city_tv;
    private EditText mAddress_detail_et;
    private TextView mSave_btn;
    private ImageButton mBack_btn;

    private HttpUtils httpUtils;
    private RequestParams params;
    private UserAddressBean userAddressBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_address);

        PushAgent.getInstance(this).onAppStart();

//        Bundle bundle = getIntent().getBundleExtra("address_data");
//        username = bundle.getString("username");
//        phone = bundle.getString("phone");
//        postcode = bundle.getString("postcode");
//        address = bundle.getString("address");
        initView();
        setListener();
        getAddress();
        //setData();
    }
    private void getAddress() {
        httpUtils = new HttpUtils();
        params = new RequestParams();
        String login_id=SharedPreferencesUtil.getData(this,"login_id","");
        params.addBodyParameter("login_id", login_id);
        String urladdress = ServerURL.httpUrl + ServerURL.userReciverAddress;
        httpUtils.send(HttpRequest.HttpMethod.POST, urladdress, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("wei", "获取地址失败");
                Toast.makeText(UserAddressUpdateActivity.this, "无网络!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("wei", "获取地址成功" + responseInfo.result);
                userAddressBean = new Gson().fromJson(responseInfo.result, UserAddressBean.class);
                setData();
            }
        });
    }
    private void setData() {
        if (!TextUtils.isEmpty(userAddressBean.getUser_name())) {
            mUsername_et.setText(userAddressBean.getUser_name());
        }
        if (!TextUtils.isEmpty(userAddressBean.getPhone())) {
            mPhone_et.setText(userAddressBean.getPhone());
        }
        if (!TextUtils.isEmpty(userAddressBean.getPost_code())) {
            mPostCode_et.setText(userAddressBean.getPost_code());
        }
        if (!TextUtils.isEmpty(userAddressBean.getAddress())) {
            String split[] = userAddressBean.getAddress().split("@@");
            if (split.length > 0) {
                mAddress_city_tv.setText(split[0]);
                mAddress_detail_et.setText(split[1]);
            }
        }
    }

    private void setListener() {
        mSave_btn.setOnClickListener(this);
        mBack_btn.setOnClickListener(this);
        mAddress_city_tv.setOnClickListener(this);
    }

    private void initView() {
        mUsername_et = (EditText) findViewById(R.id.user_address_update_username);
        mBack_btn = (ImageButton) findViewById(R.id.user_address_update_back_ib);
        mPhone_et = (EditText) findViewById(R.id.user_address_update_phone);
        mPostCode_et = (EditText) findViewById(R.id.user_address_update_post_code);
        mAddress_city_tv = (TextView) findViewById(R.id.user_address_update_choose_city);
        mAddress_detail_et = (EditText) findViewById(R.id.user_address_update_detail_address);
        mSave_btn = (TextView) findViewById(R.id.user_address_update_save);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_address_update_back_ib:
                finish();
                break;
            case R.id.user_address_update_choose_city:
                ChoiceCityDialog dialog = new ChoiceCityDialog(UserAddressUpdateActivity.this,
                        R.style.custom_dialog, R.layout.choice_city_dialog,
                        new ChoiceCityDialog.DialogListener() {
                            @Override
                            public void getCity(String city) {
                                //Toast.makeText(UserAddressUpdateActivity.this,city,Toast.LENGTH_SHORT).show();
                                mAddress_city_tv.setText(city);
                            }
                        });

                dialog.setCanceledOnTouchOutside(false);
                android.view.Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                dialog.show();
                break;
            case R.id.user_address_update_save:
                saveAddress();
                break;
            default:
                break;
        }
    }

    private void saveAddress() {
        if (TextUtils.isEmpty(mUsername_et.getText().toString().trim())) {
            Toast.makeText(UserAddressUpdateActivity.this, "请输入联系人!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mPhone_et.getText().toString().trim())) {
            Toast.makeText(UserAddressUpdateActivity.this, "请输入手机号!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mPostCode_et.getText().toString().trim())) {
            Toast.makeText(UserAddressUpdateActivity.this, "请输入邮编!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mAddress_city_tv.getText().toString().trim().contains("选择")) {
            Toast.makeText(UserAddressUpdateActivity.this, "请选择地区!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mAddress_detail_et.getText().toString().trim())) {
            Toast.makeText(UserAddressUpdateActivity.this, "请输入详细地址!", Toast.LENGTH_SHORT).show();
            return;
        }
        final String update_address = mAddress_city_tv.getText().toString().trim() + "@@" + mAddress_detail_et.getText().toString().trim();
        httpUtils = new HttpUtils();
        params = new RequestParams();
        String login_id = SharedPreferencesUtil.getData(this, "login_id", "");
        String id=userAddressBean.getId();
        if(!TextUtils.isEmpty(id)) {
            params.addBodyParameter("id", userAddressBean.getId());
        }
        params.addBodyParameter("login_id", login_id);
        params.addBodyParameter("address", update_address);
        params.addBodyParameter("post_code", mPostCode_et.getText().toString().trim());
        params.addBodyParameter("user_name", mUsername_et.getText().toString().trim());
        params.addBodyParameter("phone", mPhone_et.getText().toString().trim());
        String urladdress = ServerURL.httpUrl + ServerURL.userUpdateAddress;
        httpUtils.send(HttpRequest.HttpMethod.POST, urladdress, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Toast.makeText(UserAddressUpdateActivity.this, "无网络!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("wei", "修改地址成功" + responseInfo.result);
                try {
                    String msg = JSON.parseObject(responseInfo.result).getString("msg");
                    String status = JSON.parseObject(responseInfo.result).getString("status");
                    if (status.equals("1")) {
                        Toast.makeText(UserAddressUpdateActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();
                        String address_id = JSON.parseObject(responseInfo.result).getString("id");
                        Intent intent = new Intent();
                        intent.putExtra("update_address", update_address);
                        intent.putExtra("address_id",address_id);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
